/***************************************************************************
 *   Copyright (C) 2007 by Sebastian Bilski, Lance Cooper, Jeff Rebacz,
 *   									and Alex Zelenskiy
 *   sbilski@iit.edu
 *   lance.m.cooper@gmail.com
 *   jeff.rebacz@gmail.com
 *   voiceofanarchy@gmail.com
 *
 *	This file is part of connect3
 *
 *   connect3 is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2,
 *    as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc., 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 ***************************************************************************/

import java.awt.Color;

import junit.framework.TestCase;


public class Test_GameLogic extends TestCase {
	GameLogic gl;
	GameBoard gb;
	
	Player p1;
	Player p2;
	
	GamePiece t1;
	GamePiece t2;
	GamePiece t3;
	GamePiece t4;
	
	public Test_GameLogic(String name){
		super(name);
	}	
	
	protected void setUp(){
		gl = new GameLogic();
		gb = new GameBoard(3,3);
		
		p1 = new Player("p1", Color.RED);
		p2 = new Player("p2", Color.GREEN);
		gl.addPlayer(p1);
		gl.addPlayer(p2);

		t1 = p1.generatePiece();
		t2 = p1.generatePiece();
		t3 = p2.generatePiece();
		t4 = p2.generatePiece();
	}
	protected void tearDown(){
	}
	
	public void testDefaultConstructor(){
		gl=new GameLogic();
		assertTrue(gl.getTurnOrder()==0);
		assertTrue(gl.getTurn()==null);
	}	
	
	public void testStalemate(){
		assertFalse(gl.checkForStalemate(gb));
		assertFalse(gl.checkForWin(gb,0,0));
		int col;
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		assertFalse(gl.checkForWin(gb,0,0));
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertFalse(gl.checkForWin(gb,2,0));
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		assertFalse(gl.checkForWin(gb,1,0));
		gl.nextTurn();
		col=gb.makeMove(gl.getTurn().generatePiece(), 0);
		assertFalse(gl.checkForWin(gb,0,col));
		gl.nextTurn();
		col=gb.makeMove(gl.getTurn().generatePiece(), 1);
		assertFalse(gl.checkForWin(gb,1,col));
		gl.nextTurn();
		col=gb.makeMove(gl.getTurn().generatePiece(), 1);
		assertFalse(gl.checkForWin(gb,1,col));
		gl.nextTurn();
		col=gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertFalse(gl.checkForWin(gb,2,col));
		gl.nextTurn();
		col=gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertFalse(gl.checkForWin(gb,2,col));
		gl.nextTurn();
		assertFalse(gl.checkForStalemate(gb));
		col=gb.makeMove(gl.getTurn().generatePiece(), 0);
		assertFalse(gl.checkForWin(gb,0,col));
		assertTrue(gl.checkForStalemate(gb));
		
	}
	
	public void testWinRow(){
		
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		int col=gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertFalse(gl.checkForStalemate(gb));
		assertTrue(gl.checkForWin(gb,2,col));
	}
	
	public void testWinCol(){
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		int col=gb.makeMove(gl.getTurn().generatePiece(), 0);
		assertFalse(gl.checkForStalemate(gb));
		assertTrue(gl.checkForWin(gb,0,col));
	}
	
	public void testWinDiagonal(){
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		int col=gb.makeMove(gl.getTurn().generatePiece(), 0);
		assertFalse(gl.checkForStalemate(gb));
		assertTrue(gl.checkForWin(gb,0,col));
	}
	
	public void testWinDiagonal2(){
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		int col=gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertFalse(gl.checkForStalemate(gb));
		assertTrue(gl.checkForWin(gb,2,col));
	}
	
	public void testFullWin(){
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 1);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 2);
		gl.nextTurn();
		gb.makeMove(gl.getTurn().generatePiece(), 0);
		gl.nextTurn();
		int col=gb.makeMove(gl.getTurn().generatePiece(), 2);
		assertTrue(gl.checkForStalemate(gb));
		assertTrue(gl.checkForWin(gb,2,col));
	}	
	
	public void testRemovePlayer(){
		gl.removePlayer(1);
		assertTrue((gl.getTurn()==p1));
		assertTrue(p1.getTurnOrder()==0);
	}
	
	public void testRemoveFirstPlayer(){
		gl.removePlayer(0);
		assertTrue((gl.getTurn()==p2));		
		assertTrue(p2.getTurnOrder()==0);
	}
	
}
